package com.example.myanimator.six;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/19
 * @describe
 */
public class PathMeasureView extends View {

    private PathMeasure mPathMeasure1;
    private PathMeasure mPathMeasure2;
    private Path mPath;
    private Paint mPaint;


    public PathMeasureView(Context context) {
        super(context);
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PathMeasureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);

        mPath = new Path();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.translate(50, 50);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, 100);
        path.lineTo(100, 100);
        path.lineTo(100, 0);
        //该方法计算路径长度
        PathMeasure measure1 = new PathMeasure(path, false);
        PathMeasure measure2 = new PathMeasure(path, true);//会算上 闭合后的路径长度
        Log.d("tgw-PathMeasure", "measure1onDraw: " + measure1.getLength());
        Log.d("tgw-PathMeasure", "measure2onDraw: " + measure2.getLength());
        canvas.drawPath(path, mPaint);

        canvas.translate(150, 150);
        int firstCanvas = canvas.save();
        path = new Path();
        path.addRect(0, 0, 50, 50, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        path.addRect(-50, -50, 50, 50, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        mPaint.setColor(Color.YELLOW);
        path.addRect(-100, -100, 100, 100, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        mPaint.setColor(Color.BLUE);
        path.addRect(-120, -120, 120, 120, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        PathMeasure measure3 = new PathMeasure(path, false);
        do {
            Log.d("tgw-PathMeasure", "measure3-onDraw: " + measure3.getLength());

        } while (measure3.nextContour());


        //2.截取路径长度,measure3无效因为使用measure3.nextContour() 出完了栈
        Path gst = new Path();
        canvas.translate(500, 500);
        canvas.save();
        measure1.getSegment(-150, 560, gst, false);
        canvas.drawPath(gst, mPaint);


        path = new Path();
        path.addCircle(200, 200, 100, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        PathMeasure measureCir = new PathMeasure(path, false);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStyle(Paint.Style.FILL);
        gst = new Path();
        measureCir.getSegment(20, 300, gst, true);
        canvas.drawPath(gst, mPaint);

        canvas.restore();//恢复至上一次画布，
        canvas.restoreToCount(firstCanvas);//恢复到指定画布状态
        canvas.translate(100, 500);
        RectF rectF = new RectF(200, 200, 300, 300);
        canvas.drawArc(rectF, 20, 140, false, mPaint);
    }
}
